<?php

/**
 * Created by PhpStorm.
 * User: luzhaoyang
 * Date: 2017/3/8
 * Time: 10:21
 */
class Kdt_stat_model extends Base_Model
{
    const SALESMAN_TABLE = 'kdt_salesman';
    const SM_ORDER_TABLE = 'kdt_salesman_order';
    const GOODS_TABLE = 'kdt_goods';
    const ORDERS_TABLE = 'kdt_orders';

    public function __construct()
    {
        parent::__construct();
        $this->load->database();
    }

    public function getKdtTrades($start,$end,$step=604800)
    {
        $stop=$start+$step;
        $dur=$end-$start;
        $loop=$dur<$step?1:ceil($dur/$step);

        $this->load->model('kdt_goods_model');
        $goodsName=$this->kdt_goods_model->getGoodsName();

        $feeexcel = array();
        $feeheader=array_merge(array("日期"),$goodsName,array("独立收款总计","邮费总计","单品级优惠总计","店铺级优惠总计","订单数","客单价","实际支付","退款总额","实际销售额"));
        $feeexcel[]=$feeheader;

        $numexcel = array();
        $numheader=array_merge(array("日期"),$goodsName);
        $numexcel[]=$numheader;

        while (true){
            if ($loop == 0||$loop < 0) break;

            $date = $step == 604800?date("m.d",$start)."-".date("m.d",$stop):date("m.d",$start);
            $res=$this->kdt_goods_model->formatGSExcel($start,$stop);

            $feerow=array_merge(array($date),$res['fee']);
            $feeexcel[]=$feerow;

            $numrow=array_merge(array($date),$res['num']);
            $numexcel[]=$numrow;

            $loop--;
            $start+=$step;
            $stop +=$step;
        }

        return array("fee"=>$feeexcel,"num"=>$numexcel);
    }

    public function synSalesmanList()
    {
        $this->load->model('kdt_api_model');
        $this->load->model('kdt_push_model');
        $pageno = 1;
        while (true){
            $res = $this->kdt_api_model->getSalesmanList($pageno);
            if ($res!==false){
                if (empty($res)) {
                    echo 'total complete!'.PHP_EOL;
                    break;
                }
                else {
                    $this->kdt_push_model->updateSalesmanList($res);
                }
            }else{
                echo 'kdt api error'.PHP_EOL;
                break;
            }
            $pageno++;
        }

    }

    public function synSalesmanTrades($start,$end)
    {
        $limit = 5;
        $offset = 0;
        $counter = 1;
        $this->load->model('kdt_push_model');

        while (true){
            $phones = $this->db->select('phone,name')->from(self::SALESMAN_TABLE)->order_by('created','DESC')->limit($limit,$offset)->get()->result_array();
            if (empty($phones)) break;//遍历所有代言人后跳出

            foreach ($phones as $p){
                echo $counter." ".$p['name']." ".$p['phone'].PHP_EOL;

                $this->kdt_push_model->updateSalesmanTrades($p['phone'],$start,$end);

                $counter++;
                echo "************************".PHP_EOL;
            }

            $offset=$offset+$limit;
        }
    }

    public function synTrades($start,$end,$step=604800)
    {
        echo $start.'~'.$end.PHP_EOL;
        $start = strtotime($start);
        $end   = strtotime($end);
        $stop=$start+$step;
        $dur=$end-$start;
        $loop=$dur<$step?1:ceil($dur/$step);
        $this->load->model('kdt_push_model');

        while (true){
            if ($loop == 0||$loop < 0) break;
            $this->kdt_push_model->updateTrades($start,$stop);
            $loop--;
            $start+=$step;
            $stop +=$step;
        }
    }

    public function synSalesmanTradesByPhone($phones=array(),$start,$end)
    {
        $counter = 1;
        $this->load->model('kdt_push_model');
        if (empty($phones)) return;
        foreach ($phones as $p){
            echo $counter." ".$p.PHP_EOL;

            $this->kdt_push_model->updateSalesmanTrades($p,$start,$end);

            $counter++;
            echo "************************".PHP_EOL;
        }
    }

    public function getSalesmanTrades($start,$end,$step=604800)
    {
        $stop=$start+$step;
        $dur=$end-$start;
        $loop=$dur<$step?1:ceil($dur/$step);

        $this->load->model('kdt_goods_model');
        $goodsName=$this->kdt_goods_model->getGoodsName();

        $feeexcel = array();
        $feeheader=array_merge(array("日期"),$goodsName,array("总佣金","邀请佣金","邮费总计","商品优惠总计","交易优惠总计","订单数","邀请订单数","成单人数","邀请成单人数","客单价","支付金额","退款金额","实际销售额"));
        $feeexcel[]=$feeheader;

        $numexcel = array();
        $numheader=array_merge(array("日期"),$goodsName);
        $numexcel[]=$numheader;

        while (true){
            if ($loop == 0||$loop < 0) break;

            $date = $step == 604800?date("m.d",$start)."-".date("m.d",$stop):date("m.d",$start);
            $res=$this->kdt_goods_model->formatSMTradesExcel($start,$stop);
            if ($res===false) {
                $loop--;
                $start+=$step;
                $stop +=$step;
                continue;
            }

            $feerow=array_merge(array($date),$res['fee']);
            $feeexcel[]=$feerow;

            $numrow=array_merge(array($date),$res['num']);
            $numexcel[]=$numrow;

            $loop--;
            $start+=$step;
            $stop +=$step;
        }

        return array("fee"=>$feeexcel,"num"=>$numexcel);
    }

    public function getTradesByCityData()
    {
        $excel = array();
        $header=array("省份","城市","支付金额","退款金额","商品件数","客单价","实际支付");
        $excel[]=$header;
        $res=$this->db->select('c.province,c.city,SUM(t.payment) as payment,SUM(t.refundFee) as refundFee,SUM(t.goodsNum) as num')->from('kdt_consigns as c')
            ->join('kdt_trades as t','t.tid=c.tid','left')
            ->where_in('t.status',array(4,5,6,7))
            ->group_by('c.province,c.city')
            ->get()->result_array();
        $data = array();
        $data[] = $header;
        foreach ($res as $row){
            $resrow = array();
            $resrow[]=$row['province'];
            $resrow[]=$row['city'];
            $resrow[]=(float)$row['payment']/100;
            $resrow[]=(float)$row['refundFee']/100;
            $resrow[]=(int)$row['num'];
            $resrow[]=round(((float)$row['payment']/100-(float)$row['refundFee']/100)/((int)$row['num']),2);
            $resrow[]=(float)$row['payment']/100-(float)$row['refundFee']/100;
            $data[] = $resrow;
        }
        return $data;
    }

    public function getTradesByProvinceData()
    {
        $excel = array();
        $header=array("省份","支付金额","退款金额","商品件数","客单价","实际支付");
        $excel[]=$header;
        $res=$this->db->select('c.province,SUM(t.payment) as payment,SUM(t.refundFee) as refundFee,SUM(t.goodsNum) as num')->from('kdt_consigns as c')
            ->join('kdt_trades as t','t.tid=c.tid','left')
            ->where_in('t.status',array(4,5,6,7))
            ->group_by('c.province')
            ->get()->result_array();
        $data = array();
        $data[] = $header;
        foreach ($res as $row){
            $resrow = array();
            $resrow[]=$row['province'];
            $resrow[]=(float)$row['payment']/100;
            $resrow[]=(float)$row['refundFee']/100;
            $resrow[]=(int)$row['num'];
            $resrow[]=round(((float)$row['payment']/100-(float)$row['refundFee']/100)/((int)$row['num']),2);
            $resrow[]=(float)$row['payment']/100-(float)$row['refundFee']/100;
            $data[] = $resrow;
        }
        return $data;
    }

}